package BloodTimeHybernateTest;

import java.sql.Date;
import java.util.Calendar;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import BloodTimeHybernateDO.VampiroDO;

/**
 * 
 * @author: Cesar Armando Fuguet Tortolero
 *   
 */

public class TestVampiroDO {

    public static void main(String[] args) {
        Configuration configuration = new AnnotationConfiguration();
        configuration.configure("/BloodTimeHybernateDO/hibernate.cfg.xml");
        SessionFactory sessionFactory = configuration.buildSessionFactory();

        Session session = sessionFactory.openSession();

        // ---------------------------------------------------------------
        // Insert
        // ---------------------------------------------------------------

        session.beginTransaction();

        Calendar cal = Calendar.getInstance();
        cal.set(1988, 6, 9);

        Date fechaNac = new Date(cal.getTimeInMillis());

        VampiroDO vampiroDO = new VampiroDO();
        vampiroDO.setNombre("NombreVampiro");
        vampiroDO.setApellido("ApellidoVampiro");
        vampiroDO.setAlias("AliasVampiro");
        vampiroDO.setCorreo("CorreoVampiro@mail.com");
        vampiroDO.setContrasenia("12345");
        vampiroDO.setDinero(1000000);
        vampiroDO.setFechaNac(fechaNac);
        vampiroDO.setFuerza(10);
        vampiroDO.setDefensa(10);
        vampiroDO.setHabArmas(10);
        vampiroDO.setHabNegocios(10);
        vampiroDO.setNumEsclavos(5);
        vampiroDO.setSalud(1000);
        vampiroDO.setPtosEntrenamiento(50);
        vampiroDO.setPtosExperiencia(100);
        vampiroDO.setSangre(10);
        vampiroDO.setSigilosidad(10);

        session.save(vampiroDO);

        session.getTransaction().commit();
        session.close();

        // ---------------------------------------------------------------
        // Load
        // ---------------------------------------------------------------

        session = sessionFactory.openSession();
        session.beginTransaction();

        vampiroDO = null;
        vampiroDO = (VampiroDO) session.load(VampiroDO.class, 1);

        System.out.println("Load Vampiro 1");
        System.out.println("Id: " + vampiroDO.getId());
        System.out.println("Nombre: " + vampiroDO.getNombre());
        System.out.println("Apellido: " + vampiroDO.getApellido());
        System.out.println("Alias: " + vampiroDO.getAlias());

        session.getTransaction().commit();
        session.close();

        // ---------------------------------------------------------------
        // Query
        // ---------------------------------------------------------------

        session = sessionFactory.openSession();
        session.beginTransaction();

        Query query;

        vampiroDO = null;
        query = session.createQuery("FROM VampiroDO WHERE id = :vampiroId");
        query.setParameter("vampiroId", 1);

        vampiroDO = (VampiroDO) query.uniqueResult();

        System.out.println("Query Vampiro 1");
        System.out.println("Id: " + vampiroDO.getId());
        System.out.println("Nombre: " + vampiroDO.getNombre());
        System.out.println("Apellido: " + vampiroDO.getApellido());
        System.out.println("Alias: " + vampiroDO.getAlias());

        // ---------------------------------------------------------------
        // Delete
        // ---------------------------------------------------------------

        session.delete(vampiroDO);

        session.getTransaction().commit();
        session.close();

        // ---------------------------------------------------------------
        // Delete
        // ---------------------------------------------------------------

        session = sessionFactory.openSession();
        session.beginTransaction();

        System.out.println("List Vampiro");

        List<VampiroDO> list = session.createCriteria(VampiroDO.class).list();
        for (VampiroDO vampiro : list) {
            System.out.println("Id: " + vampiro.getId());
            System.out.println("Nombre: " + vampiro.getNombre());
            System.out.println("Apellido: " + vampiro.getApellido());
            System.out.println("Alias: " + vampiro.getAlias());
        }

        session.getTransaction().commit();
        session.close();

    }
}
